package com.jingogo.controller;

import com.jingogo.common.Result;
import com.jingogo.entity.Bill;
import com.jingogo.service.BillService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/accounting")
@Tag(name = "记账管理", description = "成本利润与账单管理接口")
public class AccountingController {
    @Autowired
    private BillService billService;

    @PostMapping("/calculateProfit")
    @Operation(summary = "自动核算成本利润", description = "自动核算成本利润")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<String> calculateProfit() {
        // TODO: 调用 service 方法
        return Result.success("核算完成");
    }

    @PostMapping("/addBill")
    @Operation(summary = "新增账单记录", description = "新增账单记录")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<String> addBill(@RequestBody Bill bill) {
        billService.addBill(bill);
        return Result.success("账单添加成功");
    }

    @PutMapping("/editBill")
    @Operation(summary = "编辑账单记录", description = "编辑账单记录")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<String> editBill(@RequestBody Bill bill) {
        billService.editBill(bill);
        return Result.success("账单编辑成功");
    }

    @DeleteMapping("/deleteBill")
    @Operation(summary = "删除账单记录", description = "删除账单记录")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<String> deleteBill(@RequestParam Long billId) {
        billService.deleteBill(billId);
        return Result.success("账单删除成功");
    }

    @GetMapping("/listBill")
    @Operation(summary = "账单记录列表", description = "获取账单记录列表")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<List<Bill>> listBill() {
        return Result.success(billService.listBill());
    }
} 